$(function () {
    $("#jqGrid").jqGrid({
        url:  baseURL + 'sys/order/list', //默认先查点位模式和进行中的订单
        datatype: "json",
         colModel: [			
			{ label: 'id', name: 'id',index: "id", /*width:20,*/ key: true ,hidden:true},
			{ label: '所属用户', name: '', width: 250, 
				  formatter: function (cellvalue, options, rowObject) { 
					  var a = rowObject.username+"【"+rowObject.cusname+"】";
					  return a;
			     }  
			},
			{ label: '上级',sortable:false, name: 'sjname', width: 250, formatter: function(value, options, row){
				var sjusername = row.sjusername;
				var sjname = row.sjname;
				if(isBlank(sjusername)){
					sjusername = " ";
				}
				if(isBlank(sjname)){
					sjname = " ";
				}
				
				return sjusername+"【"+sjname+"】"
			}},
			{ label: '兑出币种/数量',sortable:false, name: '', width: 200, formatter: function(value, options, row){
				var from = row.from;//币种
				var fromsum = row.fromsum;//数量
				if(isBlank(from)){
					from = " ";
				}
				if(isBlank(fromsum)){
					fromsum = " ";
				}
				
				return from+"【"+fromsum+"】"
			}},
			{ label: '兑入币种/数量',sortable:false, name: '', width: 200, formatter: function(value, options, row){
				var to = row.to;//币种
				var tosum = row.tosum;//数量
				if(isBlank(to)){
					to = " ";
				}
				if(isBlank(tosum)){
					tosum = " ";
				}
				
				return to+"【"+tosum+"】"
			}},
			{ label: '手续费',sortable:false, name: 'charge', width: 200},
			{ label: '成交价',sortable:false, name: 'transaction', width: 200},
			{ label: '用户余额',sortable:false, name: 'balance', width: 120},
			{ label: '发起交易时间',sortable:false, name: 'ordertime', width: 200},
			{ label: '交易结束时间',sortable:false, name: 'newovertime', width: 200},
			{ label: '交易状态', name: 'orderstatus', width: 100, formatter: function(value, options, row){
				if(value == 1){
					return "<span  style='display:inline-block;width:90px;color:red' ><b>未转款</b></span>"
				}else if(value == 2){
					return "<span  style='display:inline-block;width:90px;color:blue' ><b>已转款</b></span>"
				}else if(value == 3){
					return "<span  style='display:inline-block;width:90px;color:green' ><b>成功</b></span>"
				}else if(value == 4){
					return "<span  style='display:inline-block;width:90px;color:gray' ><b>超时</b></span>"
				}
			}},
			  
        ],
		viewrecords: true,
        height: 500,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        multiselect: false,
        autoScroll: true,
        shrinkToFit: false,//此属性用来说明当初始化列宽度时候的计算类型，如果为ture，则按比例初始化列宽度。如果为false，则列宽度使用colModel指定的宽度
        autowidth: true, //如果为ture时，则当表格在首次被创建时会根据父元素比例重新调整表格宽度。如果父元素宽度改变，为了使表格宽度能够自动调整则需要实现函数：setGridWidth
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
       	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "scroll" }); 
        }
    });
	
});
function upcust(id) {   
	$.ajax({
			type: "POST",
		    url: baseURL + "sys/customer/queryById",
		    data: {"id":id},
		    success: function(r){
				if(r.code == 0){
					$("#userid").val(r.customerEntity.id);
					$("#username").val(r.customerEntity.username);
					$("#cusname").val(r.customerEntity.cusname);
					$("#invitecode").val(r.customerEntity.invitecode);
					$("#candycount").val(r.customerEntity.candycount);
				}else{
					alert(r.msg);
				}
			}
		});
	$.ajax({
		type: "POST",
		url: baseURL + "sys/customer/queryBankCard",
		data: {"id":id},
		success: function(r){
			if(r.code == 0){
				var html = "";
				for (var int = 0; int < r.list.length; int++) {
					if(int == 0){
						$("#bankid").val(r.list[int].id);
						$("#bankcardname").val(r.list[int].bankCardName);
						$("#openbranch").val(r.list[int].openBranch);
						$("#peraccount").val(r.list[int].perAccount);
					}
					html += "<option value="+r.list[int].id+">"+r.list[int].bankAccount+"</option>";
				}
				document.getElementById("bankaccount").innerHTML = html;
			}else{
				alert(r.msg);
			}
		}
	});
	$("#upcus").modal("show");
} 
function queryBank(){
	var carid = $("#bankaccount").val();
	$.ajax({
		type: "POST",
		url: baseURL + "sys/customer/queryBankCardById",
		data: {"carid":carid},
		success: function(r){
			if(r.code == 0){
				$("#bankid").val(r.bankCardEntity.id);
				$("#bankcardname").val(r.bankCardEntity.bankCardName);
				$("#openbranch").val(r.bankCardEntity.openBranch);
				$("#peraccount").val(r.bankCardEntity.perAccount);
			}else{
				alert(r.msg);
			}
		}
	});
}
function upuserstatus(id,value) {  
	var userStatus;
	if(value == 1){
		userStatus = 2;
	}else{
		userStatus = 1;
	}
	confirm('确定要更改当前状态吗？', function(){
		$.ajax({
			type: "POST",
		    url: baseURL + "sys/customer/upuserstatus",
		    data: {"id":id,"type":userStatus},
		    success: function(r){
				if(r.code == 0){
					alert('操作成功', function(){
						vm.reload();
					});
				}else{
					alert(r.msg);
				}
			}
		});
	});
}  
function updatea(){
	var id = $("#userid").val();
	var cusname = $("#cusname").val();
	var password = $("#password").val();
	var passwordtwo = $("#passwordtwo").val();
	if(password != passwordtwo){
		alert("两次密码不一致!")
	}
	var candycount = $("#candycount").val();
	var bankid = $("#bankid").val();
	var openbranch = $("#openbranch").val();
	var peraccount = $("#peraccount").val();
	$.ajax({
		type: "POST",
	    url: baseURL + "sys/customer/updateall",
	    data: {"id":id,"cusname":cusname,"password":password,"candycount":candycount,"bankid":bankid,"openbranch":openbranch,"peraccount":peraccount},
	    success: function(r){
			if(r.code == 0){
				alert('操作成功', function(){
					vm.reload();
					$("#upcus").modal("hide");
				});
			}else{
				alert(r.msg);
			}
		}
	});
}
function Modify(id) {  

	/*$.ajax({
		type: "POST",
	    url: baseURL + "sys/customer/selechildren",
	    data: {"id":id},
	    success: function(r){
			if(r.code == 0){
				alert('操作成功', function(){
					vm.reload();
				});
			}else{
				alert(r.msg);
			}
		}
	});*/
	$("#newt").modal("show");
}  
var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			content: null,
			from: null,
			to: null,
			starttime: null,
			subordinate: null,
			endtime: null,
			orderstatus: null,
			cusname: null,
			totalorder: null
		},
		showList: true,
		title: null,
		platformEntity: {},
		totalorder:[]
	},
	methods: {
		query: function () {
			var orderstatus = $('#moshi option:selected').val();
			vm.q.orderstatus = orderstatus;
			var starttime = $("#ordertime").val();
			var endtime = $("#overtime").val();
			var startDate = new Date(starttime);    
		    var endDate = new Date(endtime);
		    if(!isBlank(starttime) && isBlank(endtime)){
		    	alert("请选择结束时间")
		    	return ;
		    }else if (isBlank(starttime) && !isBlank(endtime)){
		    	alert("请选择开始时间")
		    	return ;
		    }
		    if(startDate.getTime() > endDate.getTime()){
		    	alert("结束时间不能小于开始时间")
		    	return ;
		    }
		    vm.q.starttime = starttime;
			vm.q.endtime = endtime;
			vm.reload();
			vm.mounted1();
		},
		reload: function () {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
				postData:{'username': vm.q.cusname,"orderstatus":vm.q.orderstatus,'starttime': vm.q.starttime,'endtime': vm.q.endtime,"from":vm.q.from,"to":vm.q.to},
                 page:page 
            }).trigger("reloadGrid");
		},
		
        validator: function () {


        },
		mounted1: function(){
			$.ajax({  //统计数量
				url:baseURL + "sys/order/findCount",
				data:{'username': vm.q.cusname,"orderstatus":vm.q.orderstatus,'starttime': vm.q.starttime,'endtime': vm.q.endtime,"from":vm.q.from,"to":vm.q.to},
				type:"post",//get  
				dataType:"json",  
				success:function(r){
					vm.totalorder=r.map
				},   
			});
		}
	},
	mounted:function(){
		$.ajax({  //统计数量
			url:baseURL + "sys/order/findCount",
			type:"post",//get  
			dataType:"json",  
			success:function(r){
				console.log(r)
				vm.totalorder=r.map
				console.log(vm.totalorder)
			},   
		});
	}
});